package com.sie.controller;

import com.sie.dto.PageDto;
import com.sie.entity.Authority;
import com.sie.service.AuthorityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 权限管理控制器
 */
@RestController
@SuppressWarnings("all")
@CrossOrigin(allowedHeaders = "*", origins = "*", allowCredentials = "true")
@RequestMapping("/authority")
@Api(tags = "权限管理控制器")
public class AuthorityController {
    @Autowired
    private AuthorityService authorityService;

    @ApiOperation("分页查询")
    @PostMapping("/findByPage")
    public Map findByPage(@RequestBody PageDto<Authority> dto, HttpServletRequest request) {
        return authorityService.findByPage(dto.getData(), dto.getPageNum(), dto.getPageSize(), request);
    }

    @ApiOperation("查询所有权限项")
    @PostMapping("/findAll")
    public Map findAll(HttpServletRequest request) {
        return authorityService.findAll(request);
    }

    @ApiOperation("新增权限项")
    @PostMapping("/add")
    public Map add(@RequestBody Authority authority, HttpServletRequest request) {
        return authorityService.addAuthority(authority, request);
    }

    @ApiOperation("根据id删除权限项")
    @PostMapping("/delete")
    public Map deleteById(String id, HttpServletRequest request) {
        return authorityService.deleteById(id, request);
    }

    @ApiOperation("根据id数组批量删除权限项")
    @PostMapping("/deleteList")
    public Map deleteList(@RequestBody List<String> list, HttpServletRequest request) {
        return authorityService.deleteList(list, request);
    }

    @ApiOperation("权限名验证")
    @PostMapping("/findByName")
    public Map findByName(String authName) {
        return authorityService.findByName(authName);
    }

    @ApiOperation("修改权限项")
    @PostMapping("/update")
    public Map update(@RequestBody Authority authority, HttpServletRequest request) {
        return authorityService.update(authority, request);
    }

    @ApiOperation("查询上级父权限")
    @PostMapping("/findParent")
    public Map findParent(@RequestParam("lv") String lv) {
        return authorityService.findParentAuthority(Integer.parseInt(lv) + "");
    }


}
